<?php
/**
 * Object Relational Persistence Mapping Class for table: surgery_team_template
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class SurgeryTeamTemplate extends ORDataObject {

	/**#@+
	 * Fields of table: surgery_team_template mapped to class members
	 */
	var $template_id		= '';
	var $template_name		= '';
	var $doctor_id		= '';
	var $date		= '';
	var $modified = '';
	var $note		= '';
	var $dateformat = '%d/%m/%Y';
	/**#@-*/

	const LIMITED_COMMON_TEMPLATES = 5;	
	const NORMAL_TEMPLATE = -1;
	/**
	 * DB Table
	 */
	var $_table = 'surgery_team_template';

	/**
	 * Primary Key
	 */
	var $_key = 'template_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'SurgeryTeamTemplate';

	/**
	 * Handle instantiation
	 */
	function SurgeryTeamTemplate() {
		parent::ORDataObject();
	}

	
	/**#@+
	 * Field: date, time formatting
	 */
	function get_date() {
		return $this->_getDate('date');
	}
	function set_date($date) {
		$this->_setDate('date',$date);
	}
	/**#@-*/
	
	function listCommonTemplates($doctor_id){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "template_id, template_name, DATE_FORMAT(date,'".$this->dateformat."') AS date, DATE_FORMAT(modified,'".$this->dateformat."') AS modified, note",
			'from' 		=> "surgery_team_template", 			
			'where' 	=> "doctor_id = $doctor_id",
			'orderby'	=> "template_id",
			'limit'		=> self::LIMITED_COMMON_TEMPLATES
			);
	
		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result;
		return false;			
	}
	
	function updateTemplate($template){		
		$sql = "UPDATE surgery_team_template SET ".
			"note = '".$template['note'] .
			"', template_name = '".$template['template_name'].
			"', modified = '".date('Y-m-d').
			"' WHERE template_id = '" .$template['template_id']."'";	
		$this->_execute($sql);
	}	
	
	function removeTemplateItems($templateId){
		$sql = "DELETE FROM surgery_team_template_item where template_id = '$templateId'";
		$this->_execute($sql);
	}
	
	function getTemplate($templateID){
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "template_id, template_name, DATE_FORMAT(date,'".$this->dateformat."') AS date, DATE_FORMAT(modified,'".$this->dateformat."') AS modified, note",
			'from' 		=> "surgery_team_template", 			
			'where' 	=> "template_id = '$templateID'"
			);
	
		$ds->setup($this->_db,$sql);
		$result = $ds->toArray();
		if (!empty($result)) return $result[0];
		return $result;		
	}
	
}
?>
